package org.beholder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.*;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileCleaningTracker;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

/**
 * Designed retrieve image data sent as separate part within  Http
 * multi part POST request and store image in DB.
 */
public class MultipartUploadServlet extends AbstractServlet {
	
	private static final long serialVersionUID = 1L;

	private Logger logger = Logger.getLogger(MultipartUploadServlet.class);

    private ServletFileUpload upload;
    private DiskFileItemFactory factory;
    private FileCleaningTracker fileCleaningTracker;

    public void init() throws ServletException {

    	super.init();
    	    	
        // Create a factory for disk-based file items
        fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(this.getServletContext());
        factory = new DiskFileItemFactory();
        factory.setRepository(new File("."));
        factory.setFileCleaningTracker(fileCleaningTracker);

        // Create a new file upload handler
        upload = new ServletFileUpload(factory);

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sessionId = req.getSession().getId();
        try {
            @SuppressWarnings("unchecked")
			List<Object> items = upload.parseRequest(req);
            for (Object itemObj : items) {
                FileItem item = (FileItem) itemObj;
                if (!item.isFormField()) {
                    logger.debug("Not form field");
                    logger.debug("item.getName() = " + item.getName());
                    logger.debug("item.getFieldName() = " + item.getFieldName());
                    storeShot(getFileBytes(item.getInputStream()), sessionId);
                } else {
                	logger.debug("Form field");
                }
            }
            resp.setStatus(HttpServletResponse.SC_OK);
            req.getRequestDispatcher("/upload.jsp").forward(req,resp);
        } catch (FileUploadException e) {
        	logger.error(e, e);
        }  finally {
            closeResources();
        }
    }

    private byte[] getFileBytes(InputStream is) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            IOUtils.copy(is, os);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
            return os.toByteArray();
        } catch (Exception e) {
            logger.error(e, e);
            return new byte[0];
        }
    }


}

